package com.zhang.member.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhang.common.util.result.R;
import com.zhang.common.util.result.RUtils;
import com.zhang.member.entity.MemberReceiveAddress;
import com.zhang.member.service.IMemberReceiveAddressService;
import com.zhang.member.mapper.MemberReceiveAddressMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author hongmizfb
 * @description 针对表【my_member_receive_address(会员收货地址)】的数据库操作Service实现
 * @createDate 2023-11-27 11:35:58
 */
@Service
public class IMemberReceiveAddressServiceImpl extends ServiceImpl<MemberReceiveAddressMapper, MemberReceiveAddress>
        implements IMemberReceiveAddressService {

    @Override
    @Transactional
    public R addOne(MemberReceiveAddress memberReceiveAddress) {
        /*查询这个用户是否存在收货地址记录，如果没有就新建并且设为默认*/
        Long userId = memberReceiveAddress.getMemberId();
        List<MemberReceiveAddress> memberReceiveAddresses = listByUserId(userId);
        if (memberReceiveAddresses.size() >=10) {
            return RUtils.err("最多只能有10条收货地址");
        }
        if (memberReceiveAddresses.size() == 0) {
            memberReceiveAddress.setDefaultStatus(0);
        }
        /*如果是默认,并且已经有了记录*/
        else if (memberReceiveAddress.getDetailAddress().equals(0)) {
            /*将之前的默认地址改为普通收货地址*/
            updateDefalutToNoByUserId(userId);
        }
        save(memberReceiveAddress);
        return RUtils.successMsg("新增成功!");
    }

    @Override
    public List<MemberReceiveAddress> listByUserId(Long userId) {
        LambdaQueryWrapper<MemberReceiveAddress> wrapper = new LambdaQueryWrapper();
        wrapper.eq(MemberReceiveAddress::getMemberId, userId);
        List<MemberReceiveAddress> list = list(wrapper);
        return list;
    }

    @Override
    public void updateDefalutToNoByUserId(Long userId) {
        LambdaUpdateWrapper<MemberReceiveAddress> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(MemberReceiveAddress::getMemberId, userId)
                .eq(MemberReceiveAddress::getDefaultStatus, 0);
        MemberReceiveAddress address = new MemberReceiveAddress();
        address.setDefaultStatus(1);
        boolean update = update(address, wrapper);
    }

    @Override
    public void delByIds(List<Long> addressIds) {
        /*只有是自己的收货地址列表才能删除*/
        /*查询当前用户的收货地址列表*/
        List<MemberReceiveAddress> memberReceiveAddresses = listByUserId(StpUtil.getLoginIdAsLong());
        List<Long> collect = memberReceiveAddresses.stream().map(MemberReceiveAddress::getId).collect(Collectors.toList());
        /*如果需要删除的收货地址选项存在用户收货地址列表中，可以删除*/
        List<Long> canDel = addressIds.stream().filter(item -> {
            return collect.contains(item);
        }).collect(Collectors.toList());
        removeByIds(canDel);
    }

    @Override
    public MemberReceiveAddress getDefaultByUserId(Long  userId) {
        LambdaQueryWrapper<MemberReceiveAddress> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MemberReceiveAddress::getMemberId,userId)
                .eq(MemberReceiveAddress::getDefaultStatus,0);
        return getOne(wrapper);
    }
}




